package com.example.sxproject.controller;

import com.example.sxproject.pojo.*;
import com.example.sxproject.service.CardService;
import com.example.sxproject.service.CompanyService;
import com.example.sxproject.service.PaymentService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CardService cardService;

    @Autowired
    private PaymentService paymentService;

    //根据城市查询对应的公司列表
    @GetMapping("/companies")
    public String getCompaniesByCity(String city, HttpSession session) {
        System.out.println("城市：" + city);
        List<Company> companies = companyService.getCompaniesByCity(city);
        session.setAttribute("companies", companies);
        return "address";
    }

    @GetMapping("/search")
    public String searchCompany(String search, HttpSession session) {
        List<Company> companies = companyService.searchCompany(search);
        session.setAttribute("companies", companies);
        return "address";
    }

    //获取公司账户
    @GetMapping("/account")
    public Company getCompanyAccount(@RequestParam String city, @RequestParam String companyName) {
        return companyService.getCompanyAccount(city, companyName);
    }

    //添加insert：添加公司
    @PostMapping("/insertcompany")
    public Result addCompany(@RequestBody Company company) {
        companyService.addCompany(company);
        return Result.success();
    }

    //更新update：更新公司信息
    @PutMapping("/updatecompany")
    public Result updateCompany(@RequestBody @Validated Company company) {
        companyService.updateCompany(company);
        return Result.success();
    }

    //更新update：实现假删除
    @PutMapping("/deletecompany")
    public Result deleteCompany(@RequestBody @Validated Company company) {
        company.setCompanyStatus(2);
        companyService.deleteCompany(company);
        return Result.success();
    }

    //添加：添加一个支付记录 + 支付功能
    @RequestMapping("/pay")
    public String addPayment(PayMessage payMessage, HttpSession session) {

        System.out.println(payMessage.getCardId());
        System.out.println(payMessage.getRecardId());


        //验证银行卡密码
        if (!cardService.verifyPassword(payMessage.getCardId(), payMessage.getPassword())) {
            session.setAttribute("msg", "密码错误");
            return "payment";
        }

        //查找2个卡的信息
        Card card1 = cardService.findByIdNumber(payMessage.getCardId());//根据卡号找记录1:支付卡
        Card card2 = cardService.findByIdNumber(payMessage.getRecardId());//根据卡号找记录2：接收卡

        System.out.println(card2);

        double money = payMessage.getMoney();//转账金额

        //余额验证
        if(card1.getBalance() < money) {
            session.setAttribute("msg", "余额不足");
            return "payment";
        }

        //更新银行卡余额，实现转钱
        cardService.updateBalance(card1.getIdNumber(), card1.getBalance() - money);// 进行转钱操作
        cardService.updateBalance(card2.getIdNumber(), card2.getBalance() + money);


        //传进来的参数要包括 支付卡号、接收卡号、金额、支付类型
        Payment payment = new Payment();  //第一个交易记录
        Payment payment1 = new Payment(); // 第二个交易记录

        //流水号
        UUID uuid1 = UUID.randomUUID(); // 使用uuid生成随机32位
        UUID uuid2 = UUID.randomUUID();

        payment.setIdNumber(String.valueOf(uuid1));
        payment1.setIdNumber(String.valueOf(uuid2));

        //支付方 和 接收方
        payment.setCardId(payMessage.getCardId()); // 第一个记录的支付方 接收方
        payment.setRecardId(payMessage.getRecardId());
        payment1.setCardId(payment.getRecardId()); // 第二个 交换支付方和接收方
        payment1.setRecardId(payment.getCardId());

        //支付时间，获取当前时间，转为string类型
        String time = LocalDateTime.now().toString();
        time = time.substring(0, 19);
        time = time.replace('T', ' ');
        payment.setPayDate(time);
        payment1.setPayDate(payment.getPayDate());

        //支付金额
        payment.setMoney(payMessage.getMoney());
        payment1.setMoney(payMessage.getMoney());

        // 更改支付类型 1转入 2转出
        payment.setTransType(2);
        payment1.setTransType(1);

        //支付类型 1 转账
        payment.setPayType(1);
        payment1.setPayType(1);

        //更新所属用户
        payment.setUserId(card1.getUserId());
        payment1.setUserId(card2.getUserId());

        //当前记录状态
        payment.setPaymentStatus(1);
        payment1.setPaymentStatus(1);

        paymentService.addPayment(payment);
        paymentService.addPayment(payment1);

        session.setAttribute("msg", "");
        return "success";

    }

}
